package com.yy.QSManage.api;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.yy.QSManage.common.Result;
import com.yy.QSManage.model.dto.EnumItemDto;
import com.yy.QSManage.model.entity.FaSysEnum;
import com.yy.QSManage.service.FaSysEnumService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/FaSysEnumApi")
@Api(tags = "枚举")
public class FaSysEnumApi extends BaseApi<FaSysEnum>{

    @Autowired
    private FaSysEnumService faSysEnumService;

    @ApiOperation("获取单条")
    @ApiImplicitParam(name = "code", value = "code", required = true, dataType = "String", paramType = "path")
    @GetMapping("/getByCode/{code}")
    public Result<FaSysEnum> getByCode(@PathVariable String code){
        return Result.success(baseService.getByParam(FaSysEnum::getCode, code));
    }

    @ApiOperation("添加子选项")
    @ApiImplicitParams ({
            @ApiImplicitParam(name = "query", value = "query", required = true, dataType = "EnumItemDto", paramType = "body"),
            @ApiImplicitParam(name = "code", value = "code", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "name", value = "name", required = true, dataType = "String", paramType = "path")
    })
    @PostMapping("/addEnumItem/{code}/{name}")
    public Result addEnumItem(@RequestBody EnumItemDto query, @PathVariable String code, @PathVariable String name){
        FaSysEnum sysEnum = faSysEnumService.getByParam(FaSysEnum::getCode, code);
// 已在数据源添加时就初始化枚举数据，所以这里暂时注释
        //        if(Objects.isNull(sysEnum)){
//            JSONArray jsonArray = new JSONArray();
//            jsonArray.add(new JSONObject(query));
//            FaSysEnum saveEnum = new FaSysEnum();
//            saveEnum.setCode(code)
//                    .setName(name)
//                    .setDataJson(jsonArray.toString());
//            faSysEnumService.save(saveEnum);
//        }else{
            String dataJson = sysEnum.getDataJson();
            JSONArray jsonArray = new JSONArray(dataJson);
            // 存在则替换
//            for (Object o : jsonArray) {
//                JSONObject entries = JSONUtil.parseObj(o);
//                if(entries.getStr("value").equals(query.getValue())){
//
//                }
//            }
            // 添加选项
            jsonArray.add(new JSONObject(query));
            // 保存选项
            faSysEnumService.updateById(FaSysEnum::getDataJson, jsonArray.toString(), sysEnum.getId());
//        }
        return Result.success();
    }

    @ApiOperation("删除子选项")
    @ApiImplicitParams ({
            @ApiImplicitParam(name = "query", value = "query", required = true, dataType = "EnumItemDto", paramType = "body"),
            @ApiImplicitParam(name = "code", value = "code", required = true, dataType = "String", paramType = "path")
    })
    @PostMapping("/delEnumItem/{code}")
    public Result delEnumItem(@RequestBody EnumItemDto query, @PathVariable String code){
        FaSysEnum sysEnum = faSysEnumService.getByParam(FaSysEnum::getCode, code);
        String dataJson = sysEnum.getDataJson();
        JSONArray jsonArray = new JSONArray(dataJson);
        // 添加选项
        jsonArray.remove(new JSONObject(query));
        // 保存选项
        faSysEnumService.updateById(FaSysEnum::getDataJson, jsonArray.toString(), sysEnum.getId());
        return Result.success();
    }
}
